package com.gift.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gift.auto.JSONAuthentication;
import com.gift.entity.GiLpc;
import com.gift.entity.LpcProduct;
import com.gift.mapper.GiLpcMapper;
import com.gift.mapper.LpcTypeMapper;
import com.gift.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gift
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/gi-lpc")
public class GiLpcController extends JSONAuthentication {
    @Autowired
    GiLpcMapper giLpcMapper;
    @Autowired
    LpcTypeMapper lpcTypeMapper;
    @ApiOperation(value = "添加礼品册兑换码")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("typeId") int typeId) throws Exception {
        Random r=new Random();
        long number=r.nextInt(10000000);
        long password=r.nextInt(10000000);
        GiLpc giLpc=new GiLpc();
        giLpc.setLpNum(number);
        giLpc.setPassword(password);
        giLpc.setTypeId(typeId);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("lp_num",giLpc.getLpNum());
        if(giLpcMapper.selectOne(wrapper)!=null){
            Result error = Result.error().message("添加失败");
            this.WriteJSON(request, response, error);
        }else{
        if(giLpcMapper.insert(giLpc)>0){
            Result ok = Result.ok().data("lpcId",giLpc);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
        }
    }
    @ApiOperation(value = "查询所有兑换码")
    @PostMapping("/findAllDh")
    public void findAllDh(HttpServletRequest request, HttpServletResponse response,  @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<GiLpc> page=new Page<>(current,size);
        Page<GiLpc> pages=giLpcMapper.selectPage(page,null);
        List<GiLpc> list=pages.getRecords();
        int total=giLpcMapper.selectCount(null);
        for (GiLpc a :list){
            a.setLpcType(lpcTypeMapper.selectById(a.getTypeId()));
        }
        if (list.size()>0) {
            Result ok = Result.ok().data("lpc", list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改礼品册的状态(发货)")
    @PostMapping("/findAllDhStatusFh")
    public void findAllDhStatusFh(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("orderNum") String orderNum) throws Exception {
        GiLpc giLpc = giLpcMapper.selectById(id);
        giLpc.setStatus("已发货");
        giLpc.setOrderNum(orderNum);
        if (giLpcMapper.updateById(giLpc)>0) {
            Result ok = Result.ok().message("发货成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改礼品册的状态(送达)")
    @PostMapping("/findAllDhStatusSD")
    public void findAllDhStatusSD(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        GiLpc giLpc = giLpcMapper.selectById(id);
        giLpc.setStatus("已送达");
        if (giLpcMapper.updateById(giLpc)>0) {
            Result ok = Result.ok().message("发货成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改礼品册的状态(签收)")
    @PostMapping("/findAllDhStatusQs")
    public void findAllDhStatusQs(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        GiLpc giLpc = giLpcMapper.selectById(id);
        giLpc.setStatus("正常");
        if (giLpcMapper.updateById(giLpc)>0) {
            Result ok = Result.ok().message("发货成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

